// Toast提示框 加载中，依赖dialog.js
function toast_loading() {
	var loading= $(document).dialog({
        type : 'toast',
        infoIcon: yangtata.tpl_public_path + '/images/dialog/loading.gif',
        infoText: '正在加载中',
        autoClose: 1500
    });
	return loading;
}
// Toast提示框，依赖dialog.js
function toast(msg,type,loading) {
    var type = arguments[1]!=undefined ? arguments[1] : 0;
    var icons = ['toast0.png', 'toast1.png', 'toast2.png'];
    var setting = {
        type : 'toast',
        infoText: msg,
        autoClose: 1500,
        dialogClass: 'dialog_toast'
    };
    if (icons[type]) {
        setting.infoIcon = yangtata.tpl_public_path + '/images/dialog/' + icons[type];
    }
	if (loading){
	loading.update(setting);
	}else
	{
    var toast = $(document).dialog(setting);
    return toast;
	}
}

//锁定屏幕
function lock_touchmove(){
$("body").bind("touchmove",function(event){event.preventDefault();});	 
}
//解锁屏幕
function un_lock_touchmove(){
$("body").unbind("touchmove");
}

// ajax用户登录检查
function ajax_login(act, params) {
    var act    = arguments[0] ? arguments[0] : '';
    var params = arguments[1] ? arguments[1] : [];
	if (yangtata.is_login=='1'){
		if (act != ''){
            var fun = act;
            eval(fun)(params);
         }
	}else{
		var loading=toast_loading();
	//loading.close();
    $.getJSON(yangtata.ajax_login_url, {act: act, params: params}, function (result) {
        if (result.code == 1) {
			loading.close();
            popout = new YTATApopout({
                title: '亲，验证一下手机吧',
                showFooter: false,
                whetherClose: true,
                emptyClose: false,
                noPadding: false,
                noBackground: false
            });
            popout.setContent(result.data);
            popout.show();
        } else if (result.code == 2) {
			yangtata.is_login=1;
			loading.close();
            if (act != '') {
                var fun = act;
                eval(fun)(params);
            }
        }
    });
	}
}
//分享（处理后加载的按钮，函数处理）
function bind_share(){
	$(".J_share").off().on('click',function(){
					var ua = window.navigator.userAgent.toLowerCase();
					var share_wx='<div class="share_wx J_share_bg"></div>';
					var share_browser='<div class="share_browser J_share_bg"></div>';
					if (ua.match(/MicroMessenger/i) == 'micromessenger'){
					$('body').append(share_wx);
					}else{
					$('body').append(share_browser);
					}
					lock_touchmove();
					//关闭
					$(".J_share_bg").off().on('click',function(e){
						$(".J_share_bg").remove();
						un_lock_touchmove();
					}); 
	});
}
//直接加载函数监听分享时间，
bind_share();
/*公用 半屏表单（提问，回复，评论）*/
function show_form(tit,html){
	var htm='<div class="uu_forms_bg"></div><div class="uu_forms"><div class="t">'+tit+'<div class="cancel">取消</div><div class="confirm J_confirm" >提交</div></div>'+html+'</div>';
	$('body').append(htm);
	$('.uu_forms .focus').focus();
	lock_touchmove();
	 $(".uu_forms_bg,.cancel").click(function () {
        $(".uu_forms_bg,.uu_forms").remove();
		un_lock_touchmove();
      });
}

/*选择分类*/
function select_classify(type,pid) {
	var loading=toast_loading();
	//loading.close();
	var selectid1=$('input[name=category1]').val();
	var selectid2=$('input[name=category2]').val();
    $.getJSON(yangtata.ajax_cat_url, {type: type,pid:pid,selectid1:selectid1,selectid2:selectid2}, function (result) {
        if (result.code == 1) {
			loading.close();
			$('body').append(result.data);
			setTimeout(function(){$('.uu_cat').addClass('uu-actionsheet-toggle');}, 50);
			
        } else  {
			loading.close();
          toast(result.data);
        }
    });
}


// 提问
function ajax_question_add() {
	var loading=toast_loading();
	//loading.close();
    $.getJSON(yangtata.ajax_addquestion_url, {}, function (result) {
        if (result.code == 1) {
			loading.close();
			show_form('提问',result.data);
			$('.uu_forms').addClass('uu-actionsheet-toggle');
        } else if (result.code == 0) {
            toast(result.msg);
        }
    });
}
// 发表文章
function ajax_article_add() {
	var loading=toast_loading();
	//loading.close();
    $.getJSON(yangtata.ajax_addarticle_url, {}, function (result) {
        if (result.code == 1) {
			loading.close();
			show_form('发表文章',result.data);
			$('.uu_forms').addClass('uu-actionsheet-toggle');
        } else if (result.code == 0) {
            toast(result.msg);
        }
    });
}

// 举报
function ajax_report(params) {
	var loading=toast_loading();
	//loading.close();
    $.getJSON(yangtata.ajax_report_url, params, function (result) {
        if (result.code == 1) {
			loading.close();
			show_form('我要举报',result.data);
			$('.uu_forms').addClass('uu-actionsheet-toggle');
        } else if (result.code == 0) {
            toast(result.msg);
        }
    });
}

// 问题提问回复
function ajax_reply(params) {
	var loading=toast_loading();
	//loading.close();
    $.getJSON(yangtata.ajax_question_reply_url, params, function (result) {
        if (result.code == 1) {
			loading.close();
			show_form('回复',result.data);
			$('.uu_forms').addClass('uu-actionsheet-toggle');
        } else if (result.code == 0) {
            toast(result.msg);
        }
    });
}



// 点赞
function praise(params) {
	var loading=toast_loading();
    $.getJSON(yangtata.ajax_praise_url, params, function (result) {
        if (result.code == 1) {
            var $praise = $("#J_praise"+params.type+'_'+result.data.id);
            $praise.html(result.data.count_str);
            toast('点赞成功',1,loading);
        } else {
            toast(result.msg,0,loading);
        }
    });
}

// 收藏
function ajax_favorites(params) {
	var loading=toast_loading();
    $.getJSON(yangtata.ajax_favorites_url, params, function (result) {
        if (result.code == 1) {  // 收藏
            toast(result.msg,1,loading);
        } else {
            toast(result.msg,0,loading);
        }
    });
}

// 关注
function ajax_follow(params) {
    var loading=toast_loading();
    $.getJSON(yangtata.ajax_follow_url, params, function (result) {
        if (result.code == 1) {
            toast('关注成功',1,loading);
        } else if (result.code == 2) {
            toast('已取消关注',1,loading);
        } else {
            toast(result.msg,0,loading);
        }
    });
}

// 回复主题
function ajax_bbs_reply(params) {
    $.getJSON(yangtata.ajax_bbsreply_url, params, function (result) {
        if (result.code == 1) {
            show_form('回复',result.data);
            $('.uu_forms').addClass('uu-actionsheet-toggle');
        } else if (result.code == 0) {
            toast(result.msg);
        }
    });
}


 